package com.simdo.g768.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.simdo.g768.Bean.CellCfgBean;
import com.simdo.g768.Listener.CellClickListener;
import com.simdo.g768.R;
import com.simdo.g768.ZApplication;

import java.text.MessageFormat;
import java.util.List;

public class CellCfgAdapter extends RecyclerView.Adapter<CellCfgAdapter.ViewHolder> {

	private final List<CellCfgBean> list;
	private final List<String> bandList;
	private final CellClickListener listener;
	public final Context context;

	public CellCfgAdapter(Context context, List<CellCfgBean> list, List<String> bandList, CellClickListener listener) {
		this.context = context;
		this.list = list;
		this.bandList = bandList;
		this.listener = listener;
	}

	@NonNull
	@Override
	public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
		View view = LayoutInflater.from(viewGroup.getContext())
				.inflate(R.layout.item_cell_cfg, viewGroup, false);
		return new ViewHolder(view);
	}

	@Override
	public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
		final int pos = position;

		CellCfgBean bean = list.get(pos);

		viewHolder.tv_title.setText(MessageFormat.format("{0}{1}", context.getString(R.string.cell), bean.getCellId() + 1));
		viewHolder.tv_tip.setText(bandList.get(bean.getCellId()));
		viewHolder.rv_arfcn.swapAdapter(new CfgAdapter(this, bean.getList(), listener, pos), true);

		viewHolder.tv_import.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				listener.onClick(pos, 0);
			}
		});

		viewHolder.iv_add.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				listener.onClick(pos, 1);
			}
		});
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public int getItemCount() {
		return list.size();
	}

	boolean isWorking, isAutoPci;
	public void setWorking(boolean isWorking) {
		this.isWorking = isWorking;
	}

	@SuppressLint("NotifyDataSetChanged")
    public void setAutoPci(boolean isAutoPci) {
		this.isAutoPci = isAutoPci;
		notifyDataSetChanged();
	}

	public static class ViewHolder extends RecyclerView.ViewHolder {
		TextView tv_title, tv_tip, tv_import;
		ImageView iv_add;
		RecyclerView rv_arfcn;

        public ViewHolder(View view) {
			super(view);
            tv_title = view.findViewById(R.id.tv_title);

			tv_tip = view.findViewById(R.id.tv_tip);
			tv_import = view.findViewById(R.id.tv_import);

			iv_add = view.findViewById(R.id.iv_add);
			rv_arfcn = view.findViewById(R.id.rv_arfcn);
			rv_arfcn.setNestedScrollingEnabled(false);
			rv_arfcn.setLayoutManager(new LinearLayoutManager(view.getContext()));
		}
	}
}
